package co.sugon.log.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mysql 工具类
 *
 * */
public class MYSQLUtil {

    private static String user;
    private static String driver;
    private static String url;
    private static String password;

    static {
        try {
            user=ResourceLoader.getExtraProperties("username");
            driver=ResourceLoader.getExtraProperties("driver");
            url=ResourceLoader.getExtraProperties("url");
            password=ResourceLoader.getExtraProperties("password");
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(
                    "读取数据库配置文件失败！");
        }
    }

    /**
     * 获取数据库连接
     *
     * */
    public static Connection getConnection(){

        try {
            return DriverManager.getConnection(url,user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 释放数据库连接资源
     * */
    public static void release(Connection connection,PreparedStatement pstmt) {
        try {
            if(pstmt != null){
                pstmt.close();
            }
        }catch(Exception e) {
           e.printStackTrace();
        }finally {
            if(connection!= null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }



 /*   *//**
     * 执行新增
     * @throws SQLException
     *//*
    public boolean saveOrUpdate(String sql,List<?>params) throws SQLException{
        boolean flag=false;
        int result=-1;
        pstmt=conn.prepareStatement(sql);
        int index=1;
        if(params!=null&&!params.isEmpty()){
            for (int i = 0; i <params.size(); i++) {
                pstmt.setObject(index++, params.get(i));
            }
        }
        result=pstmt.executeUpdate();
        flag=result>0?true:false;
        return flag;
    }*/







}
